package com.sky.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Job;
import com.sky.service.JobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Api(tags = "岗位模块")
@Controller
@RequestMapping("/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @GetMapping("/job")
    public Job job(){
        return new Job();
    }

    @ApiOperation("查询所有岗位")
    @GetMapping("/list")
    public String findAll(@RequestParam(value = "content",defaultValue = "") String name,
                          @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                          Model model){
        PageHelper.startPage(pageNum,5);
        List<Job> jobList = jobService.findAll(name);
        PageInfo<Job> pageInfo = new PageInfo<>(jobList);
        model.addAttribute("pageInfo",pageInfo);
        return "job/list";
    }

    @ApiOperation("跳转添加/更新页面")
    @GetMapping("/add")
    public String toAddPage(Integer id, HttpSession session,Model model){
        if (id != null) {
            Job job = jobService.findJobById(id);
            model.addAttribute("job",job);
            session.setAttribute("msg","1");
            return "job/add";
        }else{
            session.setAttribute("msg","0");
            return "job/add";
        }
    }

    @ApiOperation("添加/更新操作")
    @PostMapping("/add")
    public String insert(Job job){
        if (job.getId() != null){
            // 更新操作
            jobService.update(job);
            return "redirect:/job/list";
        }else{
            // 添加操作
            jobService.insert(job);
            return "redirect:/job/list";
        }
    }

    @ApiOperation("删除职位")
    @GetMapping("/delete")
    public String delete(Integer id){
        jobService.delete(id);
        return "redirect:/job/list";
    }

}
